#include <iostream>
#include <sys/eventfd.h>
#include <unistd.h>
#include <string>

using std::cout;
using std::endl;

void test(char *argv[])
{
    int efd = eventfd(100, 0);
    int ret = fork();
    switch (ret)
    {
    case 0:
        for (int i{1}; argv[i] != nullptr; i++)
        {
            uint64_t j = std::stoull(argv[i]);
            int ret = write(efd, &j, sizeof(j));
            if (-1 == ret)
            {
                exit(-1);
            }
            cout << "child thread write " << j << endl;
            sleep(1);
        }
        exit(EXIT_SUCCESS);
        break;
    default:
        sleep(2);
        uint64_t j;
        int ret = read(efd, &j, sizeof(j));
        cout << "parent thread read " << j << endl;
        exit(EXIT_SUCCESS);
    }
}

int main(int args, char *argv[])
{
    test(argv);
    return 0;
}